package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	//将结构体, map切片进行序列化
	monster := Monster{
		Name:     "张三",
		Birthday: "1992-01-01",
		Sal:      8998.0,
		Skill:    "芭蕉扇",
	}

	str, error := json.Marshal(&monster)
	if error != nil {
		fmt.Println("convert failed ", error)
	}
	fmt.Println(string(str))

	fmt.Println("--------------------")

	//test2()

}

//map进行序列化
func test2() {
	var slice []map[string]interface{}
	slice = make([]map[string]interface{}, 3)

	m1 := make(map[string]interface{})
	m1["name"] = "zhangsan"
	m1["age"] = 12
	m1["birthday"] = "2021-11-11"

	m2 := make(map[string]interface{})
	m2["name"] = "lisi"
	m2["age"] = 11
	m2["birthday"] = "2020-12-12"
	slice[0] = m1
	slice[1] = m2

	res, _ := json.Marshal(slice)
	fmt.Println(string(res))

}

type Monster struct {
	Name     string  `json:"name"`
	Birthday string  `json:"birthday"`
	Sal      float64 `json:"sal"`
	Skill    string  `json:"skill"`
}
